<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
	/**
	 * 加载配置文件
	 */
    public function _initialize() {
        L(include LANG_PATH . 'common.php');
    }
	/**
	 * 安装协议
	 */
    public function index(){
    	$this->assign('step_curr', 'eula');
        $eula_html = file_get_contents(LANG_PATH . 'xieyi.html');
        $this->assign('eula_html', $eula_html);
		if (IS_POST) {
            $accept = I('post.accept',0,'int');
			
            if (!$accept) {
//              $error_msg = L('please_accept');
                $this->error( L('please_accept'));
            } else {
                $this->redirect('check');
            }
        } else {
            $this->display();
        }
    }
	
    /**
     * 环境检测
     */
    public function check() {
    	
        $flag = true;
        //检测文件夹权限
        $check_file = array(
//          './data',
//          './data/mail_tpl',
//          './data/msg_tpl',
//          './data/static',
//          './data/upload',
//          './data/config/db.php',
//          './data/config/url.php',
//          './data/config/index/config.php',
        );
        $error = array();
        foreach ($check_file as $file) {
            $path_file =  $file;
            if (!file_exists($path_file)) {
                $error[] = $file . L('not_exists');
                $flag = false;
                continue;
            }
            if (!is_writable($path_file)) {
                $error[] = $file . L('not_writable');
                $flag = false;
            }
        }
        if (!function_exists('curl_getinfo')) {
            $error[] = L('no_curl');
            $flag = false;
        }

        if (!function_exists('gd_info')) {
            $error[] = L('no_gd');
            $flag = false;
        }
//      $dir_obj = new Dir;
//      is_dir('./data/runtime') && $dir_obj->delDir('./data/runtime');

        if (!$flag) {
            $this->assign('error', $error);
            $this->assign('step_curr', 'check');
            $this->display('check');
        } else {
            $this->redirect('setconf');
        }
    }
	

    /**
     * 网站配置
     */
    public function setconf() {
        $this->assign('step_curr', 'setconf');
        if (IS_POST) {
            foreach ($_POST as $key => $val) {
                $this->assign($key, $val);
            }
            extract($_POST);
            if (!$db_host || !$db_port || !$db_name || !$db_user || !$db_prefix || !$admin_user || !$admin_email || !$admin_pass) {
                $this->assign('error_msg', L('please_input_config_info'));
                $this->display();
                exit;
            }
//          if (!$this->_is_email($admin_email)) {
//              $this->assign('error_msg', L('admin_email_format_incorrect'));
//              $this->display();
//              exit;
//          }
            if ($admin_pass != $admin_pass_confirm) {
                $this->assign('error_msg', L('admin_pass_error'));
                $this->display();
                exit;
            }
            //试着连接数据库
            $conn = @mysql_connect($db_host . ':' . $db_port, $db_user, $db_pass);
            if (!$conn) {
                $this->assign('error_msg', L('connect_mysql_error'));
                $this->display();
                exit;
            }
            $selected_db = @mysql_select_db($db_name);
            if ($selected_db) {
                //如果数据库存在 并且里面安装过   提示是否覆盖
                $query = @mysql_query("SHOW TABLES LIKE '{$db_prefix}%'");
                $was_install = false;
                while ($row = mysql_fetch_assoc($query)) {
                    $was_install = true;
                    break;
                }
                if ($was_install && !isset($force_install)) {
                    $this->assign('database_name_tip', L('db_isset'));
                    $this->display();
                    exit;
                } else {
//                  $this->_set_temp($_POST);
                    $this->redirect('install');
                }
            } else {
                if (mysql_get_server_info($conn) > '4.1') {
                    $charset = C('DEFAULT_CHARSET');
                    $sql = "CREATE DATABASE IF NOT EXISTS `{$db_name}` DEFAULT CHARACTER SET " . str_replace('-', '', $charset);
                } else {
                    $sql = "CREATE DATABASE IF NOT EXISTS `{$db_name}`";
                }
                if (!mysql_query($sql, $conn)) {
                    $this->assign('error_msg', L('create_db_error'));
                    $this->display();
                    exit;
                }
                $this->_set_temp($_POST);
                $this->redirect('install');
            }
        } else {
            $this->assign('database_name_tip', L('database_name_tip'));
            $this->assign('db_host', 'localhost');
            $this->assign('db_port', '3306');
            $this->assign('db_user', 'root');
            $this->assign('db_name', '');
            $this->assign('db_prefix', 'duosute_');

            $this->assign('admin_user', 'admin');
            $this->assign('db_pass', '');
            $this->assign('admin_pass', '');
            $this->assign('admin_pass_confirm', '');
            $this->assign('admin_email', '');
            $this->display();
        }
    }

    /**
     * 开始安装
     */
    public function install() {
        $this->assign('step_curr', 'install');
        $this->display();
    }


}